package sdk.servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Dept;
import model.Education;
import model.Emp;
import model.EmpStatus;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.etc.GetIdkey;

import vo.Json;

import dao.DeptDAO;
import dao.EmpDAO;
import dao.impl.DeptDAOImpl;
import dao.impl.EmpDAOImpl;
import dao.impl.EmpStatusDAOImpl;

public class AddEmpServlet extends HttpServlet {

	
	private static final long serialVersionUID = 1L;

	private String photoEmpPath;// 员工照片目录
	private String tempphotoEmpPath;// 员工照片临时目录

	private String idCardidPicEmpPath;

	private String EducationimgPicEmpPath ;

	

	public void init(ServletConfig config) throws ServletException {
		super.init(config);

		photoEmpPath = this.getServletContext().getRealPath("UploadPicEmp");
		// System.out.println("filePath:"+filePath);
		// filePath:D:\Tomcat\tomcat6\webapps\FileUploadAndDownload\docs
		tempphotoEmpPath = this.getServletContext().getRealPath(
				"UploadTempPicEmp");

		idCardidPicEmpPath = this.getServletContext().getRealPath(
				"UploadIdcardPicEmp");

		EducationimgPicEmpPath = this.getServletContext().getRealPath(
				"UploadDegreePicEmp");

	
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
Emp emp =new Emp();
		response.setContentType("text/html;charset=utf-8");
		String message="";
		try {

			DiskFileItemFactory factory = new DiskFileItemFactory();// 创建一个基于硬盘的FileItem工厂
			//factory.setSizeThreshold(40 * 1024);// 设置向硬盘写数据时所用的缓冲区的大小，此处为40K
			factory.setRepository(new File(tempphotoEmpPath));// 设置临时目录

			ServletFileUpload upload = new ServletFileUpload(factory);// 创建一个文件上传处理器
			upload.setSizeMax(4 * (1024 * 1024));// 设置(即所有上传数据)允许上传的文件的最大尺寸，此处为40M
			List items = upload.parseRequest(request);// 通过表单中每一个HTML标签提交的数据封装成一个FileItem对象,以List列表的形式返回
			Iterator iter = items.iterator();
			while (iter.hasNext()) {
				FileItem item = (FileItem) iter.next();
				// 判断FileItem类对象封装的数据是一个普通文本表单字段，还是一个文件表单字段，如果是普通表单字段则返回true，否则返回false。
				if (item.isFormField()) {
					// processFormField(item, out); // 处理普通的表单域
					String name = item.getFieldName();// 表单域名
					String value = item.getString("utf-8");// 表单域值
				} else {
					// processUploadedFile(item, out); // 处理上传文件
					// 获得文件上传字段中的文件名，即表单字段元素描述头中的filename属性值，如“D:\Sunset.jpg”。
					String filename = item.getName();
					int index = filename.lastIndexOf("\\");
					filename = filename.substring(index + 1, filename.length());
					
					String filename_suffix=filename.substring(filename.lastIndexOf("."));//获取文件名后缀
					long fileSize = item.getSize()/1024;// 返回该上传文件的大小（以字节为单位）

					if (filename.equals("") && fileSize == 0) return;
					System.out.println("filename:"+filename);
					File uploadFile = new File(photoEmpPath + "/"+filename_suffix);//filename将文件名替换为id
					item.write(uploadFile);// FileItem对象中保存的主体内容保存到某个指定的文件中
					
					message=filename+filename_suffix;
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
		
		emp.setPhoto(message);
		
		
	}
		

}


	


